package com.fengke.service.vo;

import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;

@SuppressWarnings("serial")
public class AccessToken implements Serializable {

	private String access_token;
	private Integer expires_in;
	private Date createdAt = new Date();
	
	private String errcode;
	private String errmsg;

	public String getAccess_token() {
		return access_token;
	}

	public void setAccess_token(String access_token) {
		this.access_token = access_token;
	}

	public Integer getExpires_in() {
		return expires_in;
	}

	public void setExpires_in(Integer expires_in) {
		this.expires_in = expires_in;
	}
	
	public String getErrcode() {
		return errcode;
	}

	public void setErrcode(String errcode) {
		this.errcode = errcode;
	}

	public String getErrmsg() {
		return errmsg;
	}

	public void setErrmsg(String errmsg) {
		this.errmsg = errmsg;
	}

	public Boolean isVaid() {
		Date now = new Date();
		long diff = now.getTime() - createdAt.getTime();
		return diff < ((expires_in - 30) * 1000);
	}
	
	public void invalidate() {
		Calendar c = Calendar.getInstance();
		long n = c.getTimeInMillis();
		n = n - expires_in * 1000 - 10000;
		c.setTimeInMillis(n);
		
		createdAt = c.getTime();
	}

	@Override
	public String toString() {
		return "AccessToken [access_token=" + access_token + ", expires_in="
				+ expires_in + ", createdAt=" + createdAt + ", errcode="
				+ errcode + ", errmsg=" + errmsg + "]";
	}


}
